<?php

namespace Admin\Controller;

use Think\Controller;
use Org\Net\Http;

class BaiduController extends Controller {
	public function index() {
		$http = new Http ();
		$geotable_id = 111203;
		$url = C ( 'BAIDU_POI_LIST' ) . "?ak=" . C ( 'BAIDU_AK' ) . "&geotable_id={$geotable_id}";
		$back = json_decode ( $http->fsockopenDownload ( $url ), 'json' );
		$total = $back ['total'];
		$url = C ( 'BAIDU_POI_LIST' ) . "?ak=" . C ( 'BAIDU_AK' ) . "&geotable_id={$geotable_id}&page_size={$total}";
		$back = json_decode ( $http->fsockopenDownload ( $url ), 'json' );
		$list = $back ['pois'];
		$this->assign ( 'list', $list );
		$this->display ();
	}
	// 百度地图转本地数据库
	public function change() {
		$http = new Http ();
		$geotable_id = 111203;
		$url = C ( 'BAIDU_POI_LIST' ) . "?ak=" . C ( 'BAIDU_AK' ) . "&geotable_id={$geotable_id}";
		$back = json_decode ( $http->fsockopenDownload ( $url ), 'json' );
		$total = $back ['total'];
		$url = C ( 'BAIDU_POI_LIST' ) . "?ak=" . C ( 'BAIDU_AK' ) . "&geotable_id={$geotable_id}&page_size={$total}";
		$back = json_decode ( $http->fsockopenDownload ( $url ), 'json' );
		$list = $back ['pois'];
		$this->assign ( 'list', $list );
		$baidu = D ( "Baidu" );
		foreach ( $list as $poi ) {
			$arr = array ();
			$arr ['id'] = $poi ['id'];
			$arr ['title'] = $poi ['title'];
			$arr ['city'] = $poi ['city'];
			$arr ['province'] = $poi ['province'];
			$arr ['district'] = $poi ['district'];
			$arr ['address'] = $poi ['address'];
			$arr ['longitude'] = $poi ['location'] [0];
			$arr ['latitude'] = $poi ['location'] [1];
			if(!$baidu->where(array('id'=>$poi ['id']))->find()){
				$baidu->add ( $arr );
			}
		}
	}
	// 给本地景区表添加地区以及
	public function test() {
		$http = new Http ();
		$geotable_id = 111203;
		$url = C ( 'BAIDU_POI_LIST' ) . "?ak=" . C ( 'BAIDU_AK' ) . "&geotable_id={$geotable_id}";
		$back = json_decode ( $http->fsockopenDownload ( $url ), 'json' );
		$total = $back ['total'];
		$url = C ( 'BAIDU_POI_LIST' ) . "?ak=" . C ( 'BAIDU_AK' ) . "&geotable_id={$geotable_id}&page_size={$total}";
		$back = json_decode ( $http->fsockopenDownload ( $url ), 'json' );
		$list = $back ['pois'];
		$this->assign ( 'list', $list );
		foreach ( $list as $poi ) {
			$province = D ( "Province" );
			$p = $province->where ( array ('province' => $poi ['province']) )->find ();
// 			if(!$p){
// 				$province->add(array('province'=>$poi ['province']));
// 			}
			$city = D ( "City" );
			$c = $city->where ( array (
					'city' => $poi ['city'] 
			) )->find ();
			if (! $c) {$city->add ( array ('city' => $poi ['city'],'province_id' => $p ['id']) );
			}
		}
	}
	public function add() {
		if (IS_POST) {
			$user = D ( 'User' );
			$data ['username'] = $_POST ['username'];
			$data ['password'] = md5 ( $_POST ['password'] );
			if ($user->create ( $data )) {
				$result = $user->add ();
			}
			// $auth_group_access = M('auth_group_access')->add(array('uid'=>$result,'group_id'=>3));
			if ($result) {
				$uid = M ( 'user' )->where ( array (
						'username' => $_POST ['username'] 
				) )->getField ( 'id' );
				$auth_group_access = M ( 'auth_group_access' )->add ( array (
						'uid' => $uid,
						'group_id' => 3 
				) );
				$this->success ( "添加成功", "index" );
			} else {
				$this->error ( "添加失败" );
			}
		} else {
			$this->display ();
		}
	}
	public function delete() {
		// $where ['id'] = I ( 'id' );
		$http = new Http ();
		$post ['geotable_id'] = '111203';
		$post ['id'] = I ( 'id' );
		$post ['ak'] = C ( "BAIDU_AK" );
		$data ['post'] = $post;
		$back = $http->fsockopenDownload ( C ( "BAIDU_POI_DELETE" ), $data );
		// file_put_contents("Public/Log/log.txt", "back:".$back."\r\n",FILE_APPEND|LOCK_EX);
		$back = json_decode ( $back, 'json' );
		if ($back ['status'] == 0) {
			$this->success ( "删除成功", U ( 'Scenic/index' ) );
		} else {
			$this->error ( "删除失败" );
		}
	}
}